﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using LitJson;

namespace Common
{
    public static class DataHelper
    {
        /// <summary>
        /// js 序列化器
        /// </summary>
        //static JavaScriptSerializer jss = new JavaScriptSerializer();

        ///// <summary>
        ///// 将 对象 转成 json格式字符串
        ///// </summary>
        ///// <param name="obj"></param>
        ///// <returns></returns>
        //public static string Obj2Json(object obj)
        //{
        //    //把集合 转成 json 数组格式字符串
        //    return jss.Serialize(obj);
        //}

        /// <summary>
        /// 返回 MD5 加密字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string MD5(string str)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
        }
        /// <summary>
        /// 将list转换为json
        /// </summary>
        /// <typeparam name="T">一个list泛型需要的类</typeparam>
        /// <param name="list">list对象</param>
        /// <returns></returns>
        public static string GetJsonString<T>(List<T> list)
        {
            return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(list);
        }

        /// <summary>
        /// 计算两个日期的时间间隔
        /// </summary>
        /// <param name="DateTime1">第一个日期和时间</param>
        /// <param name="DateTime2">第二个日期和时间</param>
        /// <returns></returns>
        static public string DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            string dateDiff = null;

            TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
            TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();
            if (ts.Days.ToString() == "0")
            {
                if (ts.Hours.ToString() == "0")
                {
                    dateDiff =

               ts.Minutes.ToString() + "分钟"
              + ts.Seconds.ToString() + "秒";

                }
                else
                {
                    dateDiff =
                ts.Hours.ToString() + "小时"
               + ts.Minutes.ToString() + "分钟"
               + ts.Seconds.ToString() + "秒";
                }

            }



            else
            {
                dateDiff = ts.Days.ToString() + "天"
               + ts.Hours.ToString() + "小时"
               + ts.Minutes.ToString() + "分钟"
               + ts.Seconds.ToString() + "秒";


            }


            return dateDiff;
        }
    }
}
